/**	
 * <br>
 * Copyright 2011 Chuan. All rights reserved.<br>
 * <br>			 
 * Package: com.chuan.mantu.sql <br>
 * FileName: DBManager.java <br>
 * <br>
 * @version
 * @author javacyc@qq.com
 * @created 2013-1-22
 * @last Modified 
 * @history
 */

package com.chuan.mantu.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * {DB业务方法}
 *  
 *  @author javacyc@qq.com
 *  @created 2013-1-22 上午09:57:10
 *  @lastModified       
 *  @history           
 */

public class DBManager {
	
	private DBHelper dbHelper;
	
	private SQLiteDatabase sqLiteDatabase;
	
	public DBManager(Context context) {
		dbHelper = new DBHelper(context);
		sqLiteDatabase = dbHelper.getWritableDatabase();
	}
	
	public void addConfig(String name,String value){
		sqLiteDatabase.beginTransaction();
		try {
			sqLiteDatabase.execSQL("INSERT INTO config VALUES(?, ?)", new Object[]{name,value});
			sqLiteDatabase.setTransactionSuccessful();
		}  finally{
			sqLiteDatabase.endTransaction();
		}
	}
	
	public void updateConfig(String name,String value){
		ContentValues cv = new ContentValues();  
        cv.put("value", value);
		sqLiteDatabase.update("config", cv, "name=?", new String[]{name});
	}
	
	public String getConfig(String name){
		Cursor c = sqLiteDatabase.query("config", new String[]{"value"}, "name=?", new String[]{name}, null, null, null);
		String value = null;
		while(c.moveToNext()){
			value = c.getString(c.getColumnIndex("value"));
		}
		c.close();
		return value;
	}
	
	public void closeDB(){
		sqLiteDatabase.close();
	}
}